<?php
namespace Home\Controller;
use Think\Controller;
class TestController extends CommonController {
	public function _initialize() {
		parent::_initialize();
		Vendor('Wxmpay.Pubhelper');
		Vendor('Wxmpay.Rexception');
		$this->wechatApi = D('WechatApi');
		Vendor('Alidayu.TopSdk');
		
		$this->commoditySrv = D('CommodityInfo');
		$this->commoditySrv->ini();
	}
	
	public function getBalanceList(){
		$userCharge = D('user_charge');
		$db_res = $userCharge->query("select * from user_charge as uc 
		left join wechat_user_info as wui on wui.user_id = uc.user_id 
		where uc.red_packet_balance>0");
		if(empty($db_res))return;
		foreach($db_res as $db_one){
			echo $db_one['user_id'].' ';
			echo $db_one['nickname'].' ';
			echo $db_one['red_packet_balance'].' ';
		}
	}
	
	public function getCode(){
		$jsApi = new \JsApi_pub();
		if (!isset($_GET['code'])) {
			$url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
			$url = $jsApi->createAuthUserUrlForCode(urlencode($url));
			sys_debug_log("***********index createAuthUserUrlForCode***********************");
			sys_debug_log($url);
			Header("Location: $url");
		} else {
			$code = $_GET['code'];
			dump($code);
		}
	}
	
	public function getUserByCode(){
		$code = $_GET['code'];
		$wat = $this->wechatApi->getWebAccessToken($code);
		if (!empty($wat['access_token']) && !empty($wat['openid'])) {
			$res = $this->wechatApi->getSnsapiUserinfo($wat['access_token'], $wat['openid']);
		}
		dump($wat);
		dump($res);
	}
	
	public function testcors() {
		header("Access-Control-Allow-Origin:*");
		
		$response['post'] = $_POST;
		$response['msg'] = '操作成功';
		$response['rsc'] = C('RESPONSE.SUCCESS');
		$this->ajaxReturn($response);
	}
	
	public function testc(){
		$t1 = $this->commoditySrv->hitLastSaleCycle('CM600004');
		// $t1 = $this->commoditySrv->hitSaleCycleInWeek('CM600004');
		dump($t1);
	}
	
	public function testua(){
		dump($_SERVER['HTTP_USER_AGENT']);
	}
	
	public function testwxpush(){
		$wechatApi = D('WechatApi');
		$url = C('HTTP_DOMAIN') .'/#/gpon?setid=1&page=home&gponid=1625';
		$title = '恭喜您，您已经成功满团，请点击详情查看购买商品';
		$remark = '您参与的抢鲜团商品数量有限，先付先得，请抓紧时间购买。';
		$wechatApi->grouponSendMsg('oGDqdwnpdKZqVBsE5lRv_q30KYCM',$url,$title,'琵琶团','朱鹏飞',$remark);
	}
	
	public function alidayu(){
		$c = new \TopClient;
		$c->appkey = C('ALIDAYU_CONFIG.app_key');
		$c->secretKey = C('ALIDAYU_CONFIG.app_secret');
		$req = new \AlibabaAliqinFcSmsNumSendRequest;
		$req->setSmsType("normal");
		$req->setSmsFreeSignName("食市");
		$req->setSmsParam("{\"code\":\"1234\"}");
		$req->setRecNum("15900851360");
		$req->setSmsTemplateCode("SMS_3980109");
		$resp = $c->execute($req);
		dump($resp);
	}
	
	public function clear_cache(){
		cookie('user_id', NULL);
		session_destroy();
		dump($_COOKIE);
		dump($_SESSION);
	}
	
	public function show_cache(){
		dump($_COOKIE);
		dump($_SESSION);
	}
	
	public function test_limit(){
		$commodityLimit = D('CommodityLimit');
		$commodityLimit->doLimitByOrder('test_order_id', 'CM100112', 1, 2, -1);
	}
	
	public function test_wx() {
		$jsApi = new \JsApi_pub();
		if(empty($_SESSION['wechat_openid']) && isWXBrowser()){
			if (!isset($_GET['code'])){
				$url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
				$url = $jsApi->createAuthUserUrlForCode(urlencode($url));
				Header("Location: $url"); 
			}else{
				$code = $_GET['code'];
				$wat = $this->wechatApi->getWebAccessToken($code);
				$this->wechatApi->getSnsapiUserinfo($wat['access_token'],$wat['openid']);
			}
		}
	}
	
	public function test_wx2() {
		$this->wechatApi->getCommonAccessToken();
	}
	
	public function test(){
		Vendor('Msg.SmsMsgService');
		$smsMsgSrv = new \SmsMsgService();
		$smsMsgSrv->sendCaptcha("15900851360");
	}
	
	function test20151130(){
		$paySrv = D('PaymentInfo');
		//$flowId,$tradeNo,$payType
		$res = $paySrv->completeOnlinePayFlow('OPF565269192112073009',C('WXMPAY_PREFIX') .'111111111111111',C('PAYMENT_TYPE.WXMPAY'));
		dump($res);
	}
	

	function test2(){
		$sale_cycle = 'w4|14:00:00|172800,w1|14:00:00|172800';
		$LastSaleCycle = $this->hitLastSaleCycle($sale_cycle);
		dump($LastSaleCycle);
	}
	function hitLastSaleCycle($sale_cycle){
		$sale_cycle_arr = explode(',',trim($sale_cycle));
		$new_arr = array();
		foreach($sale_cycle_arr as $cycle){
			$cycle_arr = explode('|',$cycle);
			$collect_week = str_replace("w","",$cycle_arr[0]);
			if($collect_week-date("w")>=0){
				$collect_datetime = date("Y-m-d",strtotime("+".($collect_week-date("w"))." day"))." ".$cycle_arr[1];
				if($collect_datetime<=date("Y-m-d H:i:s")){
					$collect_datetime = date("Y-m-d H:i:s",strtotime($collect_datetime)+60*60*24*7);
				}
			}else{
				$collect_datetime = date("Y-m-d",strtotime("+".($collect_week-date("w")+7)." day"))." ".$cycle_arr[1];
			}
			$dispatch_datetime = date("Y-m-d H:i:s",strtotime($collect_datetime)+$cycle_arr[2]);
			$new_arr[] = array('collect_datetime'=>$collect_datetime,'dispatch_datetime'=>$dispatch_datetime);
		}
		$new_arr = array_sort($new_arr,'collect_datetime','asc');
		return $new_arr[0];
	}
}